package com.itheima.service.impl;

import com.itheima.entity.LoginQueryParam;
import com.itheima.entity.LoginResponse;
import com.itheima.mapper.LoginMapper;
import com.itheima.service.LoginService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    LoginMapper loginMapper;

    @Override
    public LoginResponse login(LoginQueryParam param) {
        LoginResponse lr=loginMapper.login(param);
        //自定义有效载荷
        Map<String, Object> claims=new HashMap<>();
        claims.put("id",lr.getId());
        claims.put("username",lr.getUsername());

        //调用 JWT 工具类，生成令牌 token
        String jwt = JwtUtils.generateJwt(claims);

        lr.setToken(jwt);
        return lr;

    }
}
